package com.globalwave.common.exception;

import java.util.List;
import java.util.Locale;

import com.globalwave.common.cache.CodeHelper;


/**
 * @author virgil
 */
public class BusinessException extends RuntimeException {
	private static final long serialVersionUID = 1L;
	
	public static String MESSAGE_KEY_SYSTEM = "err.common"; 
	
	protected String messageKey = "";
	protected Object[] params;
	private Locale locate = null;
	
	
	/** 提示信息集(仅提示) */
    private List<BusinessException> messageList = null;
    
    /** 选择确定信息集(可选择是/否) */
    private List<BusinessException> confirmList = null;
    
    /** 主任授权信息集 */
    private List<BusinessException> validSuperList = null;
    
    /** 错误信息集 */
    private List<BusinessException> errorList = null;
    
    /** 页面信息集 */
    private List<BusinessException> displayList = null;
	
	
	public BusinessException(Exception e,String messageKey){
		super(e);
		this.messageKey = messageKey;
	}
	
	public BusinessException(Exception e,String messageKey, Object... params){
		super(e);
		this.messageKey = messageKey;
		this.params = params;
	}
	
	public BusinessException(String messageKey){
		this.messageKey = messageKey;
	}
	
	public BusinessException(String messageKey, Object... params){
		this.messageKey = messageKey;
		this.params = params;
	}
	

	public void setMessageKey(String messageKey) {
		this.messageKey = messageKey;
	}
	
	public Object[] getParams() {
		return params;
	}
	public void setParams(Object[] params) {
		this.params = params;
	}
	
	@Override
	public String getMessage() {
		String msg = super.getMessage() ;
		if(msg == null) {
			return getLocalizedMessage() ;
		} else {
			return msg ;
		}
	}
	
	@Override
	public String getLocalizedMessage() {
		String msg = null;
		try {
			msg = CodeHelper.getString("Message", "value_", this.messageKey) ;
		} catch (Exception e) {
			
		}
		
		if (msg == null) {
			msg = this.messageKey ;
		} else {
			msg = String.format(locate, msg, params) ;
		} 
		
		return msg ;
	}

	public Locale getLocate() {
		return locate;
	}

	public void setLocate(Locale locate) {
		this.locate = locate;
	}

	public List<BusinessException> getMessageList() {
		return messageList;
	}

	public void setMessageList(List<BusinessException> messageList) {
		this.messageList = messageList;
	}

	public List<BusinessException> getConfirmList() {
		return confirmList;
	}

	public void setConfirmList(List<BusinessException> confirmList) {
		this.confirmList = confirmList;
	}

	public List<BusinessException> getValidSuperList() {
		return validSuperList;
	}

	public void setValidSuperList(List<BusinessException> validSuperList) {
		this.validSuperList = validSuperList;
	}

	public List<BusinessException> getErrorList() {
		return errorList;
	}

	public void setErrorList(List<BusinessException> errorList) {
		this.errorList = errorList;
	}

	public List<BusinessException> getDisplayList() {
		return displayList;
	}

	public void setDisplayList(List<BusinessException> displayList) {
		this.displayList = displayList;
	}

	public String getMessageKey() {
		return messageKey;
	}
	
	@Override
	public String toString() {
		return super.toString() + "[" + getMessage() + "]";
	}
}
